package com.hgx.su.example.common.springboot.redis.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgx.su.common.pure.dto.CommonPageReqDto;
import com.hgx.su.common.pure.dto.CommonPageResDto;
import com.hgx.su.common.springboot.mybatis.plus.config.util.PageHelperUtil;
import com.hgx.su.example.common.springboot.redis.dao.UserMapper;
import com.hgx.su.example.common.springboot.redis.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Description:
 * author: HGX
 * DateTime: 2021-02-04 16:49
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate ;


    @Cacheable(value = "common-springboot-redis-example", keyGenerator = "wiselyKeyGenerator")
    public List<User> getAll1() {
        return userMapper.selectList(null);
    }

    @Cacheable(value = "common-springboot-redis-example", key = "'getAll2'")
    @DS("slave_1")
    public List<User> getAll2() {
        return userMapper.selectList(null);
    }


    public CommonPageResDto<User> pageHelper(CommonPageReqDto reqDto) {
        // pageHelper
        com.github.pagehelper.Page<User> objects = PageHelperUtil.pageStart(reqDto);
        userMapper.selectList(new LambdaQueryWrapper<User>().ne(User::getId, -1));
        return PageHelperUtil.converterToCommonPageDto(objects, 10);
    }

    public Page<User> pagePlus() {
        return userMapper.selectPage(new Page<>(2, 3), null);
    }

}
